package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.yyds.common.MyJSON;
import com.yyds.common.R;
import com.yyds.mapper.NewsCommentsMapper;
import com.yyds.pojo.NewsComments;
import com.yyds.service.NewsCommentsService;
import com.yyds.vo.NewsCommentsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service("newsCommentsService")
//@SuppressWarnings({"all"})//抑制警告
public class NewsCommentsServiceImpl implements NewsCommentsService {

    @Autowired
    NewsCommentsMapper newsCommentsMapper;

    public void setNewsCommentsMapper(NewsCommentsMapper newsCommentsMapper) {
        this.newsCommentsMapper = newsCommentsMapper;
    }

//    @Override
//    public MyJSON<NewsComments> selectAllNewsCommentsService(NewsComments newsComments, Integer page, Integer limit) {
//        System.out.println("通过分页查询新闻的评论的全部信息 : page= "+page+" , limit= "+limit);
//        int count = newsCommentsMapper.findNewsComments(newsComments).size();
//        PageHelper.startPage(page,limit);
//        List<NewsComments> list = newsCommentsMapper.findNewsComments(newsComments);
//        MyJSON<NewsComments> commentsMyJSON = new MyJSON<>();
//        commentsMyJSON.setCode(0);
//        commentsMyJSON.setMsg("success");
//        commentsMyJSON.setCount((long)count);
//        commentsMyJSON.setData(list);
//        return commentsMyJSON;
//    }
    @Override
    public MyJSON<NewsCommentsVo> selectAllNewsCommentsService(NewsCommentsVo newsCommentsVo, Integer page, Integer limit) {
        System.out.println("通过分页查询新闻的评论的全部信息 : page= "+page+" , limit= "+limit);
        int count = newsCommentsMapper.selectCompleteNewsCommentsMapper(newsCommentsVo).size();
        if (page == null || page == 0 ) {
            page = 1;
            limit = 10;
        }
        PageHelper.startPage(page,limit);
        List<NewsCommentsVo> list = newsCommentsMapper.selectCompleteNewsCommentsMapper(newsCommentsVo);
        MyJSON<NewsCommentsVo> commentsMyJSON = new MyJSON<>();
        commentsMyJSON.setCode(0);
        commentsMyJSON.setMsg("success");
        commentsMyJSON.setCount((long)count);
        commentsMyJSON.setData(list);
        return commentsMyJSON;
    }

    @Override
    public R insertNewsCommentsService(NewsComments newsComments) {
        System.out.println("添加新闻模块的评论的全部信息 : newsComments= "+newsComments);
        newsComments.setComments_createtime(new Date());
        int i = newsCommentsMapper.insertNewsCommentsMapper(newsComments);
        if (i > 0 ) {
            return R.ok().message("添加成功");
        }else{
            return R.error().message("添加失败");
        }
    }

    @Override
    public R selectNewsCommentsService(Integer comments_id) {
        return R.ok().data("comments",newsCommentsMapper.selectNewsCommentsMapper(comments_id));
    }

    @Override
    public R updateNewsCommentsService(NewsComments newsComments) {
        return R.ok().message("更改"+newsCommentsMapper.updateNewsCommentsMapper(newsComments)+"条新闻");
    }

    @Override
    public R deleteNewsCommentsService(Integer comments_id) {
        return R.ok().message("删除了"+newsCommentsMapper.deleteNewsCommentsMapper(comments_id)+"条新闻");
    }

    @Override
    public R deleteAllNewsCommentsService(List<NewsComments> list) {
        List<Integer> collect = list.stream().map(NewsComments::getComments_id).collect(Collectors.toList());
        int[] comments_ids = new int[collect.size()];
        Iterator<Integer> iterator = collect.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            comments_ids[i] = iterator.next();
        }
        int num = newsCommentsMapper.deleteAllNewsCommentsMapper(comments_ids);
        return R.ok().message("删除了"+num+"条新闻");
    }



}
